package com.xqls.biz.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xqls.bean.ExportBean;
import com.xqls.bean.FlowMeterBean;
import com.xqls.bean.UserBean;
import com.xqls.biz.FlowMeterBiz;
import com.xqls.dal.model.FlowMeter;
import com.xqls.dal.model.Login;
import com.xqls.redis.Constants;
import com.xqls.redis.RedisCache;
import com.xqls.redis.RedisKeyEnum;
import com.xqls.service.CompanyService;
import com.xqls.service.FlowMeterService;
import com.xqls.service.LoginService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DateFormatter;
import com.xqls.util.DictionaryEnums;
import com.xqls.util.DateFormatter;
import com.xqls.util.DownloadAct;
import com.xqls.util.PurviewUtil;
import com.xqls.util.VelocityUtil;

@Service
public class FlowMeterBizImpl implements FlowMeterBiz {
	
	@Autowired private FlowMeterService service;
	
	@Autowired private LoginService loginService;
	
	private static Logger logger = Logger.getLogger(FlowMeterBiz.class);

	@Autowired
	private RedisCache<String, Object> redisCache;
	
	@Autowired
	private CompanyService companyService;
	@Override
	public CommonResult getFlowByPage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = new CommonResult();
		try {
			commonPage.setPageData(service.getFlowMetermap(commonPage.pageToMap()));
			Map<String,Object> showMap=new HashMap<String,Object>();
			Subject subject = SecurityUtils.getSubject();
			String username = subject.getPrincipal().toString();
			Map<String, Object> loginMap = new HashMap<String, Object>();
			loginMap.put("userName", username);
			List<Login> loginList = loginService.getLoginByMap(loginMap);
			for (Login login : loginList) {
				if(DictionaryEnums.EDIT_BOTTON.getMsg().equals(login.getPermission().getPermName())){
					showMap.put("editButton", "yes");
			boolean isHave = PurviewUtil.getIsHavePurview(DictionaryEnums.EDIT_BOTTON);
			if(isHave){
				showMap.put("editButton", "yse");
				}
			}
			showMap.put("flowmeterList",commonPage.getPageData());
			commonPage.setHtml(VelocityUtil.getHtmlByMap("flowmeter/flowmeterListData.vm",showMap,request,response,request.getSession().getServletContext()));
			result.setIsSuccess(true);
			}
		}catch (Exception e) {
			logger.error("分页条件查询流量抄表异常",e);
		}
		return result;
	}
	
	@Override
	public CommonResult deleteFlowRole(List<Integer> ids){
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			if(ids.size()>0){
				success = service.deleteFlowMeter(ids);
			}
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("批量删除流量抄表异常",e);
		}
		return result;
	}
	
//增加
	@Override
	public CommonResult addFlowRole(FlowMeterBean flowMeterBean){
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
				if(flowMeterBean != null){
					flowMeterBean.setCloseMonthString(DateFormatter.format(flowMeterBean.getCloseMonth()));
					FlowMeter flowMeter= service.selectByTime(flowMeterBean.getCloseMonthString(), flowMeterBean.getDeviceNo());
					if(flowMeter==null||"".equals(flowMeter)){
						success = service.add(flowMeterBean);
					}else{
						success=false;
					}
				}
				result.setIsSuccess(success);
		} catch (Exception e){
			logger.error("编辑流量抄表异常",e);
		}
		return result;
	}
//修改
	@Override
	public CommonResult editFlowRole(FlowMeterBean flowMeterBean) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
				if(flowMeterBean != null){
						success = service.update(flowMeterBean);
					}else{
						success=false;
					}
				result.setIsSuccess(success);
		} catch (Exception e){
			logger.error("编辑流量抄表异常",e);
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> queryByMap(Map<String, Object> map){
		List<Map<String, Object>> beanMap = null;
		try {
			beanMap = service.getFlowMetermap(map);
		} catch (Exception e) {
			logger.error("根据map查询流量抄表异常",e);
		}
		return beanMap;
	}

	@Override
	public FlowMeterBean queryByKey(Integer key) {
		FlowMeterBean bean = null;
		try {
			bean = service.flowMeter(key);
		} catch (Exception e){
			logger.error("根据主键查询流量抄表异常",e);
		}
		return bean;
	}

	@Override
	public List<FlowMeter> getList() {
		List<FlowMeter> FlowMeterList=null;
		try {
			FlowMeterList=this.service.getList();
		} catch (Exception e) {
			logger.error("查询企业异常",e);
		}
		return FlowMeterList;
	}

	@Override
	public FlowMeter queryByClose(String close,String deviceNo){
		FlowMeter bean = null;
		try {
			bean = service.selectByClose(close,deviceNo);
		} catch (Exception e){
			logger.error("根据结算时间和设备编号查询流量抄表异常",e);
		}
		return bean;
	}

	@Override
	public List<FlowMeter> selectBySmonth(Date close, String deviceNo){
		List<FlowMeter> flow = null;
		try {
			flow = service.selectBySmonth(close, deviceNo);
		} catch (Exception e) {
			logger.error("根据结算时间和设备编号查询上个月记录异常",e);
		}           
		return flow;
	}

	@Override
	public CommonResult selectBySmonth(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = new CommonResult();
		try {
			commonPage.setPageData(service.selectBySmonth(commonPage.pageToMap()));
			Map<String,Object> showMap=new HashMap<String,Object>();
			showMap.put("costList",commonPage.getPageData());
			commonPage.setHtml(VelocityUtil.getHtmlByMap("flowmeter/costListData.vm",showMap,request,response,request.getSession().getServletContext()));
			result.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("分页条件查询操作记录异常",e);
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> queryselectflowcost(Integer companyId, String  closeMonth) {
		List<Map<String, Object>> bean = null;
		try {
			bean = service.selectflowcost(companyId, closeMonth);
		} catch (Exception e){
			logger.error("根据企业编号和结算时间查询费用生成异常",e);
		}
		return bean;
	}

	@Override
	public void getListByMap(HttpServletRequest request, HttpServletResponse response,Map<String, Object> map) {
		try {
			List<String> strList=null;
			List<Map<String, Object>> mapList = this.service.getListByMap(map);
			List<List<String>> recordList=new ArrayList<List<String>>();
			if(mapList.isEmpty())
			{
			}
			else
			{
				String sheetName="流量抄表统计";
				ExportBean exportBean=new ExportBean();
				exportBean.setSheetName(sheetName);
				List<String> thNames=new ArrayList<String>();
				thNames.add("序号");
				thNames.add("取水口");
				thNames.add("结算月份");
				thNames.add("记录时间");
				thNames.add("累计流量（吨）");
				
				for(int i=0;i<mapList.size();i++){
					strList=new ArrayList<String>();
					strList.add(String.valueOf(mapList.get(i).get("flowId")));
					strList.add(String.valueOf(mapList.get(i).get("companyName")));
					strList.add(String.valueOf(mapList.get(i).get("closeMonth")));
					strList.add(String.valueOf(mapList.get(i).get("recordTime")));
					strList.add(String.valueOf(mapList.get(i).get("accumuLate")));
					recordList.add(strList);
				}
				exportBean.setThNames(thNames);
				exportBean.setContainHeadTdColspan(thNames.size());
				exportBean.setContainHeadTdHtml("流量抄表统计");
				exportBean.setDataLIst(recordList);
				
				DownloadAct.excelDownload(request, response, exportBean);
			}
		} catch (Exception e) {
			logger.error("导出流量抄表统计异常", e);
		}
	}

	@Override
	public FlowMeter selflow(FlowMeterBean flowMeterBean){
		FlowMeter bean=null;
		try {
			flowMeterBean.setCloseMonthString(DateFormatter.format(flowMeterBean.getCloseMonth()));
			bean=service.selectByTime(flowMeterBean.getCloseMonthString(), flowMeterBean.getDeviceNo());
		} catch (Exception e){
			logger.error("根据结算时间和设备编号查询单月所有数据异常",e);
		}
		return bean;
	}

	@Override
	public CommonResult getByPage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult result = new CommonResult();
		try {
			commonPage.setPageData(service.getFlowMetermap(commonPage.pageToMap()));
			Map<String,Object> showMap=new HashMap<String,Object>();
			Subject subject = SecurityUtils.getSubject();
			String username = subject.getPrincipal().toString();
			Map<String, Object> loginMap = new HashMap<String, Object>();
//			loginMap.put("userName", username);
//			List<Login> loginList = loginService.getLoginByMap(loginMap);
//			for (Login login : loginList) {
//				if(DictionaryEnums.EDIT_BOTTON.getMsg().equals(login.getPermission().getPermName())){
//					showMap.put("editButton", "yes");
//			boolean isHave = PurviewUtil.getIsHavePurview(DictionaryEnums.EDIT_BOTTON);
//			if(isHave){
//				showMap.put("editButton", "yse");
//				}
//			}
			showMap.put("flowmeterList",commonPage.getPageData());
			commonPage.setHtml(VelocityUtil.getHtmlByMap("flowmeter/flowListData.vm",showMap,request,response,request.getSession().getServletContext()));
			result.setIsSuccess(true);
//          }
		}catch (Exception e) {
			logger.error("分页条件查询流量抄表异常",e);
		}
		return result;
	}

	
}
